package com.hd.trans.framework.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hd.trans.R;
import com.hd.trans.utils.CommonUtil;
import com.hd.trans.utils.StringUtils;
import com.hd.trans.widgets.XEditText;
import com.hd.trans.widgets.filter.EditLenInputFilter;

import java.lang.ref.WeakReference;

/**
 * Author:  yang
 * Date:    2020/01/09 17:54
 * Desc:    编辑框()
 */
public class EditorDlg extends BaseHdDlg implements View.OnClickListener {


    //    @BindView(R.id.tv_title)
  private  TextView tvTitle;
    //    @BindView(R.id.et_content)
    private XEditText etContent;
    //    @BindView(R.id.tv_cancel)
    private TextView tvCancel;
    //    @BindView(R.id.tv_confirm)
    private TextView tvConfirm;


    private static final String ARG_VALUE = "arg_value";

    private String mValue = "";


    private OnInputChangedListener mOnInputChangedListener;

    private MyHandler mHandler;

    @Override
    public void onClick(View v) {
        /*switch (v.getId()) {
            case R.id.tv_confirm:
                if (null == etContent.getText() || StringUtils.isEmpty(etContent.getText().toString().trim())) {
//                    ToastUtils.showError(R.string.tips_empty_content);
                    Toast.makeText(getContext(), R.string.tips_empty_content, Toast.LENGTH_SHORT).show();
                } else {
                    dismiss();
                    if (mOnInputChangedListener != null)
                        mOnInputChangedListener.onInputChanged(etContent.getText().toString());
                }
                break;

            case R.id.tv_cancel:
                dismiss();
                break;
        }*/

        if(v.getId() == R.id.tv_confirm){
            if (null == etContent.getText() || StringUtils.isEmpty(etContent.getText().toString().trim())) {
//                    ToastUtils.showError(R.string.tips_empty_content);
                Toast.makeText(getContext(), R.string.tips_empty_content, Toast.LENGTH_SHORT).show();
            } else {
                dismiss();
                if (mOnInputChangedListener != null)
                    mOnInputChangedListener.onInputChanged(etContent.getText().toString());
            }
        }else if(v.getId() == R.id.tv_cancel){
            dismiss();
        }
    }


    public interface OnInputChangedListener {

        void onInputChanged(String value);
    }

    protected int getAnimStyle() {
        return R.style.BaseDialogStyle;
    }


    public static EditorDlg newInstance(String input) {
        EditorDlg instance = new EditorDlg();
        Bundle args = new Bundle();
        args.putString(ARG_VALUE, input);
        instance.setArguments(args);
        return instance;
    }

    public static EditorDlg newInstance() {
        return newInstance("");
    }

    public EditorDlg setOnInputChangedListener(OnInputChangedListener mOnInputChangedListener) {
        this.mOnInputChangedListener = mOnInputChangedListener;
        return this;
    }

    @Override
    protected void initDialog(Dialog dialog) {
        mHandler = new MyHandler(this);
        setDialogGravity(Gravity.CENTER);
        View view = inflateContentView(R.layout.dialog_editor_new);
        tvTitle = view.findViewById(R.id.tv_title);
        etContent = view.findViewById(R.id.et_content);
        tvCancel = view.findViewById(R.id.tv_cancel);
        tvConfirm = view.findViewById(R.id.tv_confirm);
        tvCancel.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);
        Bundle args = getArguments();
        if (args != null) {
            mValue = args.getString(ARG_VALUE);
        }
        tvTitle.setText(R.string.title_editor);
//        etContent.setFilters(new InputFilter[]{new EditLenInputFilter(14)});

        InputFilter[] origFilters = etContent.getFilters();
        InputFilter[] newFilters;
        if (origFilters == null || origFilters.length == 0) {
            newFilters = new InputFilter[]{new EditLenInputFilter(1000)};
        } else {
            newFilters = new InputFilter[origFilters.length + 1];
            System.arraycopy(origFilters, 0, newFilters, 0, origFilters.length);
            newFilters[origFilters.length] = new EditLenInputFilter(1000);
        }
        etContent.setFilters(newFilters);

        etContent.setText(mValue);
//        etContent.setHint(R.string.hint_nick);
        etContent.setSelection(mValue.length() > 1000 ? 1000 : mValue.length());

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        dialog.setContentView(view);
        mHandler.postDelayed(() -> CommonUtil.showKeyboard(etContent), 300);
    }

    public void setEtContent(String content){
        etContent.setText(content);
    }

    @Override
    public void dismiss() {
        CommonUtil.hideKeyboard(etContent);
        super.dismiss();
    }

    static class MyHandler extends Handler {
        WeakReference<EditorDlg> mDialog;

        public MyHandler(EditorDlg dialog) {
            mDialog = new WeakReference<>(dialog);
        }

        @Override
        public void handleMessage(Message msg) {
            EditorDlg dlg = mDialog.get();
//            switch (msg.what) {
//                case INTERVAL_UPDATE:
//                    if(dlg.mCurInterval<dlg.mTotalInterval)
//                        dlg.onIntervalChanged(dlg.mCurInterval,dlg.mTotalInterval);
//                    else{
//                        dlg.dismiss();
//                    }
//                    break;
//            }
        }
    }
}
